#ifndef JSVM_RUMTIME_COMMON_H
#define JSVM_RUMTIME_COMMON_H
#include <glog/logging.h>
#include "jsi/jsi.h"

namespace rnjsvm{
using Runtime = facebook::jsi::Runtime;
using Value = facebook::jsi::Value;
using Symbol = facebook::jsi::Symbol;
using BigInt = facebook::jsi::BigInt;
using String = facebook::jsi::String;
using Pointer = facebook::jsi::Pointer;
using PropNameID = facebook::jsi::PropNameID;
using Object = facebook::jsi::Object;
using HostObject = facebook::jsi::HostObject;
using NativeState = facebook::jsi::NativeState;
using Function = facebook::jsi::Function;
using HostFunctionType = facebook::jsi::HostFunctionType;
using PreparedJavaScript = facebook::jsi::PreparedJavaScript;
using Instrumentation = facebook::jsi::Instrumentation;
using Buffer = facebook::jsi::Buffer;
using Array = facebook::jsi::Array;
using WeakObject = facebook::jsi::WeakObject;
using ArrayBuffer = facebook::jsi::ArrayBuffer;
using MutableBuffer = facebook::jsi::MutableBuffer;
using JSError = facebook::jsi::JSError;
}

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)

#define GET_AND_LOG_STRING(status, result)                        \
    {                                                             \
      char str[200] = {0};                                        \
      OH_JSVM_GetValueStringUtf8(env, result, str, 200, nullptr); \
      LOG(INFO) << "call status: " << (uint32_t)status << str;    \
    }

#define CALL_JSVM(env, thecall)                                    \
    {                                                              \
      auto JSVM_STATUS = thecall;                                  \
      if (unlikely(JSVM_STATUS != JSVM_OK)) {                      \
        JSVM_Value ERROR_RESULT_JSVM;                              \
        OH_JSVM_GetAndClearLastException(env, &ERROR_RESULT_JSVM); \
        /*GET_AND_LOG_STRING(JSVM_STATUS, ERROR_RESULT_JSVM);*/    \
        }                                                          \
    }
#define CALL_JSVM_AND_THROW(thecall)        \
    {                                       \
        if (unlikely(thecall != JSVM_OK)) { \
            DLOG(INFO) << "call failed";    \
            ThrowError();                   \
        }                                   \
    }

#endif

